﻿Imports MeoTom.Core.BLL
Imports MeoTom.Core

Namespace Control

    Partial Public Class ArticleCategoryEdit1
        Inherits UserControl
        Private ReadOnly _cat As New Categories()
        Private ReadOnly _util As New Utilities.Common()

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim previousPage As String = ""
                If Request.QueryString.ToString = vbNullString Then
                    previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO"))
                Else
                    previousPage = Server.UrlEncode(Request.ServerVariables("PATH_INFO") & "?" & Request.QueryString.ToString())
                End If
                _util.ValidateLogin(previousPage)

                LoadParentCat()
                LoadCatDetail()
            End If
        End Sub

        Public Sub LoadCatDetail()
            _cat.CatID = Integer.Parse(Request.QueryString("CatID").ToString)
            _cat.GetByID()

            txtCatIndex.Text = _cat.CatIndex
            txtDesc.Text = _cat.Description
            txtTitle.Text = _cat.Title
            DropDownList1.SelectedValue = _cat.SubCat
        End Sub

        Public Sub LoadParentCat()
            _cat.SubCat = 0
            DropDownList1.DataSource = _cat.GetParentCat()
            DropDownList1.DataTextField = "Title"
            DropDownList1.DataValueField = "CatID"
            DropDownList1.DataBind()

            DropDownList1.Items.Insert(0, "None")
            DropDownList1.SelectedValue = "None"
        End Sub

        Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            Try
                _cat.Title = txtTitle.Text
                _cat.Description = txtDesc.Text
                _cat.CatID = Integer.Parse(Request.QueryString("CatID").ToString)
                _cat.CatIndex = Integer.Parse(txtCatIndex.Text)
                If DropDownList1.SelectedValue = "None" Then
                    _cat.SubCat = 0
                Else
                    _cat.SubCat = Integer.Parse(DropDownList1.SelectedValue)
                End If
                _cat.Update()
                _cat.WriteCategory()
                Exit Try
            Catch ex As Exception
            End Try
            Response.Redirect("/mt-inside/ArticleCategory.aspx")
        End Sub
    End Class
End Namespace